package com.booksystem.campuslibrarymanagementsystem.controller.approve;

import com.booksystem.campuslibrarymanagementsystem.commen.request.approverequest.ViolationDispose.ViolationAdd;
import com.booksystem.campuslibrarymanagementsystem.commen.request.approverequest.ViolationDispose.ViolationAlter;
import com.booksystem.campuslibrarymanagementsystem.commen.request.approverequest.ViolationDispose.ViolationDelete;
import com.booksystem.campuslibrarymanagementsystem.commen.request.approverequest.ViolationDispose.ViolationList;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.entity.SystemTable;
import com.booksystem.campuslibrarymanagementsystem.entity.ViolationTable;
import com.booksystem.campuslibrarymanagementsystem.service.ViolationTableService;
import com.booksystem.campuslibrarymanagementsystem.utils.JwtUtil;
import com.booksystem.campuslibrarymanagementsystem.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("/approval")
@Api(tags = "违规处理")
@RestController
public class ViolationDisposeController {

    SystemTable systemTable = new SystemTable();
    ViolationTable violationTable = new ViolationTable();

    @Autowired
    ViolationTableService violationTableService;

    @PostMapping("/ViolationDisList")
    @ApiOperation(value = "违规信息", notes = "已处理违规用户信息")
    public Result ViolationDisList(@RequestBody ViolationList violationList) {
        Map<String, Object> parsed = JwtUtil.tokenPre(violationList.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            if (identity.equals("approval")) {
                String status = violationTable.getStatus();
                if (status == null || status.isEmpty()) {
                    return Result.fail("没有已处理的违规账户信息");
                }

                List<ViolationTable> statulist = null; // 调用Service层的querydis方法
                if ("已通过".equals(status)) {
                    statulist = violationTableService.querydislist(status);
                } else if ("未通过".equals(status)) {
                    statulist = violationTableService.querydislist(status);
                } else {
                    return Result.fail("无效的状态值");
                }
                // 检查列表是否为空
                if (statulist.isEmpty()) {
                    return Result.fail("没有找到状态为 '" + status + "' 的借阅记录");
                } else {
                    return Result.success(statulist, statulist.size());
                }

            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }

    @PostMapping("/ViolationAdd")
    @ApiOperation(value = "违规账户增加", notes = "违规账户增加")
    public Result ViolationAdd(@RequestBody ViolationAdd violationAdd) {
        Map<String, Object> parsed = JwtUtil.tokenPre(violationAdd.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("approval")) {
                String content = violationAdd.getViolationType();//违规原因
                int bookid = violationAdd.getBookId();//违规相关书籍
                int fineamount = violationAdd.getFineamount();//罚款金额
                String time = TimeUtil.getTime();

                ViolationTable vio = new ViolationTable();
                vio.setAccount((String) account); // 存储账户ID
                vio.setFineamount(fineamount);
                vio.setBookId(bookid);//涉及书籍
                vio.setViolationDate(content);
                vio.setViolationDate(time);
                vio.setStatus("未处理");
                int a = violationTableService.savav(vio);
                if (a > 0) {
                    return Result.success(vio, 1);
                } else {
                    return Result.fail((String.format("该账户添加失败：%s", account)));
                }


            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }

    @PostMapping("/ViolationDelete")
    @ApiOperation(value = "删除违规信息", notes = "账户id，删除违规信息")
    public Result deleteViolation(@RequestBody ViolationDelete violationDelete) {
        Map<String, Object> parsed = JwtUtil.tokenPre(violationDelete.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("approval")) {
                int bookid = violationDelete.getBookId();  //图书id
                int hh = violationTableService.deleteByAccountAndBookid((String) account, bookid);
                // 首先检查该用户是否有违规记录
                if (hh > 0) {
                    return Result.success("成功删除账户的该条违规记录", 1);
                }
                return Result.fail(String.format("没有该账户的违规记录：%s", bookid));
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }

    @PostMapping("/ViolationUpdate")
    @ApiOperation(value = "违规信息修改", notes = "修改图书id，账户，违规信息，罚款金额")
    public Result ViolaionAlter(@RequestBody ViolationAlter violationAlter) {
        Map<String, Object> parsed = JwtUtil.tokenPre(violationAlter.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("approval")) {
                int bookid = violationAlter.getBookId();  //图书id
                String account1 = violationAlter.getAccount();//违规用户id
                ViolationTable violationTable = violationTableService.queryByvio((String) account1, bookid);
                if (violationTable != null) {
                    System.out.println(violationAlter);
                    String violationType = violationTable.getViolationType();
                    int fineamount = violationTable.getFineamount();
                    violationTableService.updetaViolaionAlter(violationType, fineamount);
                    return Result.success(200, "修改成功", 1);
                } else {
                    return Result.fail("身份权限不足");
                }
            } else {
                return Result.fail("请输入正确token值");
            }
        }
        return Result.fail("修改失败");
    }
}
